#include "Log.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <malloc.h>

#define LOG_BUF_SIZE 512
#define CONSOLE
#define LOG_ON

void Log::OutputLog(char * msg, ...)
{
#ifdef LOG_ON
	char logName[256] = "/home/cgf/log.txt";
	FILE * fp = fopen(logName, "ab");
    if(fp == NULL)
    {
        printf("log.txt is not existing\n");
        return ;
    }

	struct tm *newtime;
	time_t aclock;

	time(&aclock);
	newtime = localtime(&aclock);
	char szTime[32];

	strftime(szTime, sizeof(szTime), "[%Y-%m-%d %H:%M:%S] ", newtime);
#ifndef CONSOLE
	fputs(szTime, fp);
#else
	printf(szTime);
#endif

	va_list ap;
	char buf[LOG_BUF_SIZE];
	va_start(ap, msg);
	vsnprintf(buf, LOG_BUF_SIZE, msg, ap);
	va_end(ap);
#ifndef CONSOLE
	fputs(buf, fp);
	fputs("\n", fp);
#else
	printf(buf);
	printf("\n");
#endif

	if (fp)
	    fclose(fp);
#endif
}
